import { IFeidaoAiBrowserComponent } from '@dfeidao/atom-web/interfaces';
import get from '@dfeidao/atom-web/local/get';
import set from '@dfeidao/atom-web/local/set';
// import render_tpl from '@dfeidao/atom-web/render/data-render';
import render from '@dfeidao/atom-web/render/render';
// import { fromString, place, Position, remove } from '@dfeidao/core/dom';
// import { IHandle } from '@dfeidao/core/on';
import a015 from './a015';
import P01 from './P01';

interface Aa {
	param: {
		id: object[];
	};
	order: HTMLElement;
	id: string;
}
interface Bb {
	param: {
		id: HTMLElement;
	};
	order: Array<{}>;
}
interface Cc {
	field_name: string;
	field_value: string[];
	field: string;
	order_key: string;
}


export default async function a017(fd: IFeidaoAiBrowserComponent, args: { currentTarget: HTMLElement }) {
	const name2title = get(fd, 'name2title');
	// 升降序
	// console.log('--------a017---------');
	const field = args.currentTarget.dataset.feidaoId;
	const v = args.currentTarget.dataset.feidaoSort;

	const condition_node = get(fd, 'condition_node') as Aa;
	const condition_evt = get(fd, 'condition_evt') as Bb;
	// 渲染条件
	if (condition_node.order) {		// 先删除order节点 'condition_'+key
		delete condition_node.order;
		// (condition_evt.order[0]).destroy(); 不需要用！！！
		delete condition_evt.order;
		const o_node = fd.data.node.querySelector("#condition_order");
		// remove(o_node);
		o_node.parentNode.removeChild(o_node);	// 找到删除的子元素，然后使用其 parentNode 属性来找到父元素：
	}
	const tpl_d = { field_name: "order" } as Cc;
	tpl_d.field = "order";
	tpl_d.field_value = [field, v];
	tpl_d.order_key = name2title[field];
	// const tpl_str = render_tpl(tpl_d, P01);
	// const node = fd.data.node.querySelector('[data-feidao-presentation="P01"]');
	// place(fromString(tpl_str), Position.LastIn, node);
	render(fd, tpl_d, P01, 'P01', 'lastin');
	condition_node.order = fd.data.node.querySelector('[data-feidao-id="order"]');
	// condition_evt.order = fd.parse_event(condition_node.order);


	const obj = {};
	obj[field] = v;
	set(fd, 'page_no', 1);
	set(fd, 'sort', obj);
	set(fd, 'condition_evt', condition_evt);
	set(fd, 'condition_node', condition_node);
	await a015(fd);
}
